// miniprogram/pages/picture/picture.js
const app = getApp()
import Filters from '../../utils/util.js'
let interstitialAd = null
Page({

    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        modalName: null,
        name:'',
        posterConfig: {

        },
        nullBG:true
    },

    onLoad: function (options) {
        if (wx.createInterstitialAd) {
            interstitialAd = wx.createInterstitialAd({
                adUnitId: 'adunit-90fa84e2a9183ddb'
            })
            interstitialAd.onLoad(() => { 
                if (interstitialAd) {
                    interstitialAd.show().catch((err) => {
                        console.error(err)
                    })
                }
            })
            interstitialAd.onError((err) => { })
            interstitialAd.onClose(() => { })
        }


        let SDATA = []
        let H = 50;
        for (let i = 0; i < 32; i++) {
            SDATA.push({
                x: 50 + (i % 8) * 82,
                y: H + 40 + parseInt(i / 8) * 82,
                width: 82,
                height: 82,
                borderWidth: 1,
                borderColor: '#F1F1F1'
            })
        }
        let TTIT = [
            /****** 棋谱 *******/
            {
                x: 50,
                y: H,
                fontSize: 32,
                text: '棋谱',
                color: '#8799A3'
            },
            {
                x: 120,
                y: H,
                fontSize: 20,
                text: 'Chess Score',
                color: '#8799A3'
            },
            /****** 棋谱 *******/
            /****** 详情 *******/
            {
                x: 50,
                y: H + 430,
                fontSize: 32,
                text: '棋子详情',
                color: '#8799A3'
            },
            {
                x: 190,
                y: H + 430,
                fontSize: 20,
                text: 'Details',
                color: '#8799A3'
            }
            /****** 详情 *******/
        ]



        let roleImg = [];
        let roleData = app.globalData.detailsData;
        let effectData = app.globalData.effectData;
        let roleName = [];
        let roleDesNumber = 1;
        let addsdata = [];
        let h = H;
        let allTexts = [], allBlocks = [], allimages = []

        roleName.push(
            {
                x: 580,
                y: h,
                fontSize: 20,
                text: '棋子总价：' + app.globalData.money + '$',
                color: '#8799A3'
            }
        )

        for (let i = 0; i < roleData.length; i++) {
            /****** 棋谱 *******/
            if (roleData[i].id) {
                roleImg.push({
                    width: 82,
                    height: 82,
                    x: 50 + (i % 8) * 82,
                    y: h + 40 + parseInt(i / 8) * 82,
                    url: roleData[i].cloudImg
                })
                for (let j = 0; j < roleData[i].level; j++) {
                    roleImg.push({
                        width: 16,
                        height: 16,
                        x: 52 + (i % 8) * 82 + j * 16,
                        y: h + 42 + parseInt(i / 8) * 82,
                        url: '../../images/star.png'
                    })
                }
                roleName.push({
                    x: 90 + (i % 8) * 82,
                    y: h + 114 + parseInt(i / 8) * 82,
                    fontSize: 19,
                    text: roleData[i].name,
                    textAlign: 'center',
                    width: 82,
                    lineHeight: 24,
                    color: '#ffffff',
                    zIndex: 99999
                })
                /****** 棋谱 *******/
                /****** 详情 *******/
                addsdata.push({
                    x: 50,
                    y: h + 460 + roleDesNumber * 24,
                    height: 24,   
                    text: {
                        width: 130,
                        fontSize: 20,
                        lineHeight: 24,
                        text: roleData[i].name,
                        textAlign: 'left',
                        color: Filters.color(roleData[i].price)
                    },   
                })
                addsdata.push({
                    x: 180,
                    y: h + 460 + roleDesNumber * 24,
                    height: 24,
                    text: {
                        width: 130,
                        fontSize: 20,
                        lineHeight: 24,
                        text: roleData[i].level + '级',
                        textAlign: 'left',
                        color: '#666666'
                    }
                })
                addsdata.push({
                    x: 310,
                    y: h + 460 + roleDesNumber * 24,
                    height: 24,
                    text: {
                        width: 130,
                        fontSize: 20,
                        lineHeight: 24,
                        text: roleData[i].race,
                        textAlign: 'left',
                        color: '#666666'
                    }
                })
                addsdata.push({
                    x: 440,
                    y: h + 460 + roleDesNumber * 24,
                    height: 24,
                    text: {
                        width: 130,
                        fontSize: 20,
                        lineHeight: 24,
                        text: roleData[i].profession,
                        textAlign: 'left',
                        color: '#666666'
                    }
                })
                addsdata.push({
                    x: 570,
                    y: h + 460 + roleDesNumber * 24,
                    height: 24,
                    text: {
                        x:0,
                        y:0,
                        width: 130,
                        fontSize: 20,
                        lineHeight: 24,
                        text: Filters.text(roleData[i].price),
                        textAlign:'left',
                        color: Filters.color(roleData[i].price),
                    }
                })
                /****** 详情 *******/    
                roleDesNumber ++;
            }
        }

        //棋盘加详情高度
        h = h + 430 + 20 + roleDesNumber * 24 + 70;
        /****** 种族*****/
        let titleRec = [
            {
                x: 50,
                y: h,
                fontSize: 32,
                text: '种族',
                color: '#8799A3'
            },
            {
                x: 120,
                y: h,
                fontSize: 20,
                text: 'Chess Race',
                color: '#8799A3'
            },
        ]
        let pic1 = [
            {
                x: 50,
                y: h + 30,
                width: 650,
                height: 200,
                url: options.pic1
            }
        ]
        /****** 种族*****/
        h = h + 200 + 30 + 40
        /****** 职业*****/
        let titleOccupation = [
            {
                x: 50,
                y: h,
                fontSize: 32,
                text: '职业',
                color: '#8799A3'
            },
            {
                x: 120,
                y: h,
                fontSize: 20,
                text: 'Chess Occupation',
                color: '#8799A3'
            },
        ]
        let pic2 = [
            {
                x: 50,
                y: h + 30,
                width: 650,
                height: 200,
                url: options.pic2
            }
        ]
        /****** 职业*****/
        h = h + 200 + 30 + 40
        /******效果 *******/
        if (effectData.length <= 0) {
            effectData.push('无效果')
        }
        let titleEffect = [
            {
                x: 50,
                y: h,
                fontSize: 32,
                text: '触发效果',
                color: '#8799A3'
            },
            {
                x: 190,
                y: h,
                fontSize: 20,
                text: 'Chess Effect',
                color: '#8799A3'
            },
        ]
        let textEffect = []
        h = h + 60
        for (let i = 0; i < effectData.length;i++){
            textEffect.push({
                x:50,
                y:h + i * 24,
                fontSize: 20,
                lineHeight: 24,
                text: effectData[i],
                textAlign: 'left',
                color: '#666666'
            })
        }
        /******效果 *******/

        /*****战力分析 ****/
        let titleRader = [], pic3 =[]
        if(options.pic3){
            h = h + effectData.length * 24 + 40
            titleRader = [
                {
                    x: 50,
                    y: h,
                    fontSize: 32,
                    text: '战力分析',
                    color: '#8799A3'
                },
                {
                    x: 190,
                    y: h,
                    fontSize: 20,
                    text: 'Combat Effectiveness',
                    color: '#8799A3'
                },
            ]
            pic3 = [
                {
                    x: 50,
                    y: h + 30,
                    width: 650,
                    height: 340,
                    url: options.pic3
                }
            ]
            h = h + 30 + 340
        }
        /*****战力分析 ****/

        let pic4 = [
            {
                x: 0,
                y: h + 20,
                width: 750,
                height: 275,
                url: '../../images/share.jpg'
            }
        ]
        
        allimages = roleImg.concat(pic1).concat(pic2).concat(pic3).concat(pic4)
        allTexts = TTIT.concat(titleRec).concat(roleName).concat(titleOccupation).concat(titleEffect).concat(textEffect).concat(titleRader).concat(titleRader)
        allBlocks = SDATA.concat(addsdata)

        this.setData({
            posterConfig: {
                width: 750,
                height: h,
                backgroundColor: '#ffffff',
                debug: false,
                images: allimages,
                blocks: allBlocks,
                texts: allTexts,
            }
        })
    },
    showModal(e) {
        this.setData({
            modalName: e
        })
    },
    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    changeName(e){
        this.setData({
            name: e.detail.value
        })
    },
    onPosterSuccess(res) {
        let that = this
        this.setData({
            nullBG: false
        })
        wx.reportAnalytics('save_picture', {
            number: 0,
        });
        wx.saveImageToPhotosAlbum({
            filePath: res.detail,
            success:()=>{
                wx.showToast({
                    icon: 'none',
                    title: '已保存相册，快去分享吧',
                })
            },
            fail:(e)=>{
                console.log(e)
                wx.getSetting({
                    success(res) {
                        console.log(res.authSetting['scope.writePhotosAlbum'])
                        if (res.authSetting['scope.writePhotosAlbum'] == false) {
                            that.showModal('DialogModal1')
                        } else {

                        }
                    }
                })
            }
        })
    }
})